package com.example.lib.utils.sp

import android.content.Context
import com.example.lib.LibApp


object SpUtils {
    private const val tag = "-1"
    private const val empty = ""

    private val Sp by lazy {
        LibApp.appContext.getSharedPreferences("user", Context.MODE_PRIVATE)
    }
    private val Edit by lazy { Sp.edit() }

    fun putBoolean(name: String, msg: Boolean?) = msg?.let { Edit.putBoolean(name, it).apply() }

    fun getBoolean(name: String, default: Boolean = false): Boolean = Sp.getBoolean(name, default)

    fun putString(name: String, msg: String?) = msg?.let { Edit.putString(name, it).apply() }

    fun getString(name: String, default: String = empty): String = Sp.getString(name, default) ?: ""

    fun putInt(name: String, msg: Int?) = msg?.let { Edit.putInt(name, it).apply() }

    fun getInt(name: String, default: Int = tag.toInt()): Int = Sp.getInt(name, default)

    fun putFloat(name: String, msg: Float?) = msg?.let { Edit.putFloat(name, it).apply() }

    fun getFloat(name: String, default: Float = 0f): Float = Sp.getFloat(name, default)

    fun putLong(name: String, msg: Long?) = msg?.let { Edit.putLong(name, it).apply() }

    fun getLong(name: String, default: Long = tag.toLong()): Long = Sp.getLong(name, default)

    fun remove(name: String) = Edit.remove(name).commit()

    fun logout() {

    }
}